package com.xbongbong.saas.factory.publicrule;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.ClueErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.ErrorMessageConstant;
import com.xbongbong.saas.domain.entity.ClueRuleEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Objects;

/**
 * @author long
 * @version v1.0
 * @date 2020/12/1 16:52
 * @since v1.0
 */
public class OperateNoCommunicateClueRuleValue implements IOperateClueRuleValue{

    private static final Logger LOG = LoggerFactory.getLogger(OperateNoCommunicateClueRuleValue.class);

    @Override
    public void verify(String ruleValue) throws XbbException {
        JSONObject ruleObject = null;
        try {
            ruleObject = JSON.parseObject(ruleValue);
        } catch (Exception e) {
            // ignore
        }

        if (ruleObject == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015);
        }

        JSONArray restrict;
        Integer type;
        JSONArray clueStatus;

        try {
            type = ruleObject.getInteger("type");
            clueStatus = ruleObject.getJSONArray("clueStatus");
            restrict = ruleObject.getJSONArray("restrict");
        } catch (Exception e) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015);
        }

        boolean isNotType = !Objects.equals(type, 0) && !Objects.equals(type, 1) && !Objects.equals(type, 2);
        if (type == null || isNotType) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015, ErrorMessageConstant.SETTING_TYPE_ERROR);
        }

        if (clueStatus == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015, ErrorMessageConstant.CLUE_STATUS_ERROR);
        }

        if (restrict == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015);
        }

        for (Object restrictObject : restrict) {
            JSONObject restrictJson;
            JSONArray array;
            JSONArray users;

            try {
                restrictJson = (JSONObject) restrictObject;
                array = restrictJson.getJSONArray("array");
                users = restrictJson.getJSONArray("users");
            } catch (Exception e) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015);
            }

            if (users != null && users.size() == 0) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015, ErrorMessageConstant.SPECIAL_STAFF_NOT_BE_EMPTY);
            }
            if (Objects.equals(type, 2)) {
                for (Object object : array) {
                    JSONObject json;
                    String key = null;
                    Integer value = null;

                    try {
                        json = (JSONObject) object;
                        key = json.getString("key");
                        value = json.getInteger("value");
                    } catch (Exception e) {
                        throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015);
                    }
                    if (key == null || Objects.equals(key, "[]")) {
                        throw new XbbException(ClueErrorCodeEnum.API_ERROR_1111002);
                    }
                    if (value != null && value < 1) {
                        throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015, ErrorMessageConstant.DAYS_GREATER_ZERO);
                    }
                    if (value != null && value > 99999) {
                        throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015, ErrorMessageConstant.MAXIMUM_DAYS);
                    }
                }
                continue;
            }
            for (Object object : array) {
                JSONObject json;
                Integer key = null;
                Integer value = null;

                try {
                    json = (JSONObject) object;
                    key = json.getInteger("key");
                    value = json.getInteger("value");
                } catch (Exception e) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015);
                }
                if (key == null || key < 0) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015);
                }
                if (value != null && value < 1) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015, ErrorMessageConstant.DAYS_GREATER_ZERO);
                }
                if (value != null && value > 99999) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100015, ErrorMessageConstant.MAXIMUM_DAYS);
                }
            }
        }

    }

    @Override
    public Object toObject(ClueRuleEntity rule) {
        if (rule == null) {
            return null;
        }
        Object retObject;

        try {
            retObject = JSON.parse(rule.getRuleValue());
        } catch (Exception e) {
            retObject = null;
            LOG.error("corpid:" + rule.getCorpid() + " ruleId:" + rule.getId() + "------json格式错误\n " + rule.getRuleValue());
        }

        return retObject;
    }

    @Override
    public List<String> getAllSpecialUser(Object ruleObject) {
        return OperateCheckRuleValue.checkSpecialUser(ruleObject);
    }

    /**
     * 获取规则配置中的标签Id
     *
     * @param ruleObject
     * @return
     */
    @Override
    public List<Long> getClueLabelIds(Object ruleObject) {
        return OperateCheckRuleValue.checkClueLabelIds(ruleObject);
    }

    @Override
    public Integer getStatus(ClueRuleEntity clueRule) {
        if (clueRule != null) {
            return clueRule.getStatus();
        }
        return null;
    }

    @Override
    public JSONArray getClueStatus(ClueRuleEntity clueRule) {
        JSONObject retObject = (JSONObject) toObject(clueRule);
        if (retObject == null) {
            return null;
        }
        return retObject.getJSONArray("clueStatus");
    }

    @Override
    public Integer getType(ClueRuleEntity clueRule) {
        JSONObject retObject = (JSONObject) toObject(clueRule);
        if (retObject == null) {
            return null;
        }
        return retObject.getInteger("type");
    }

    @Override
    public JSONArray getRestrict(ClueRuleEntity clueRule) {
        JSONObject retObject = (JSONObject) toObject(clueRule);
        if (retObject == null) {
            return null;
        }
        return retObject.getJSONArray("restrict");
    }
}
